//
//  ReceiptView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

class ReceiptView: UIView {
    
    // MARK: - Outlets
    
    lazy private var timeLbl: UILabel = {
        let lbl = UILabel()
        lbl.sizeToFit()
        lbl.numberOfLines = 2
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        lbl.textColor = .black
        return lbl
    }()
    
    lazy private var avatarImg: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        img.image = UIImage(named: "avatar")
        return img
    }()
    
    lazy private var headerView: UIView = {
        let view = UIView()
        view.backgroundColor = .whiteBlueColor
        
        let label = UILabel()
        view.addSubview(label)
        label.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        label.textAlignment = .left
        label.textColor = .black
        label.sizeToFit()
        label.text = "Автообъявления"
        label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().inset(16)
        }
        return view
    }()
    
    lazy var infoStackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.alignment = .leading
        stack.distribution = .fillEqually
        return stack
    }()
    
    // MARK: - Properties
    
    private var currentItem: HistoryItem!
    
    
    
    // MARK: - Methods
    
    private func updateUIData (item: HistoryItem) {
        self.setTimeLblText(dateTimestamp: item.timestamp)
        /// info details:
        let hints = ["№ квитанции: ", "Объявление №: ", "Итого оплачено: ", "ФИО покупателя: ", "Оплачено с: "]
        let values = [String(item.kvintasiaNumber), String(item.id), item.cost, item.fullName]
        self.addRows(data: hints, isValue: false)
        self.addRows(data: values, isValue: true)
    }
    
    private func setTimeLblText (dateTimestamp: Int) {
        let timeFormatter = FormatterDate()
        let date = Date(timeIntervalSince1970: TimeInterval(dateTimestamp))
        let str = timeFormatter.getDateString(date: date, at: "dd.MM.yyyy HH:mm")

        let attrString = NSMutableAttributedString(string: str)
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 4
        attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: str.count))
        timeLbl.attributedText = attrString
    }
    
    private func addRows (data: [String], isValue: Bool) {
        let columnStack = UIStackView()
        columnStack.axis = .vertical
        columnStack.alignment = .top
        columnStack.distribution = .fill
        columnStack.spacing = 16.0
        for item in data {
            let itemLbl = UILabel()
            itemLbl.font = UIFont.systemFont(ofSize: 14, weight: isValue ? .bold : .medium)
            itemLbl.textColor = isValue ? .black : .greyColor
            itemLbl.text = item
            itemLbl.sizeToFit()
            columnStack.addArrangedSubview(itemLbl)
        }
        if (isValue) {
            columnStack.addArrangedSubview(self.paymentView())
        }
        infoStackView.addArrangedSubview(columnStack)
    }
    
    
    private func paymentView () -> UIView {
        let paymentView = UIView()
        
        /// Icon of payment system
        let paymentIcon = UIImageView()
        paymentView.addSubview(paymentIcon)
        paymentIcon.image = currentItem.paymentIcon
        paymentIcon.contentMode = .scaleAspectFit

        /// Info about card number
        let paymentLabel = UILabel()
        paymentLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        paymentLabel.textColor = .black
        paymentLabel.textAlignment = .left
        paymentLabel.sizeToFit()
        paymentLabel.text = currentItem.paymentName + ", " + currentItem.paymentCard
        paymentView.addSubview(paymentLabel)
        
        paymentLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(paymentIcon.snp.trailing).offset(8)
            make.top.equalToSuperview()
            make.height.equalTo(20)
        }
        paymentIcon.snp.makeConstraints { (make) in
            make.width.equalTo(28)
            make.height.equalTo(18)
            make.leading.equalToSuperview()
            make.top.equalToSuperview()
        }
        return paymentView
    }
    
    
    // MARK: - Init
    
    init (historyItem: HistoryItem) {
        super.init(frame: .zero)
        self.currentItem = historyItem
        self.addViews()
        self.addViewConstraints()
        self.updateUIData(item: historyItem)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    
    private func addViews () {
        self.addSubview(timeLbl)
        self.addSubview(avatarImg)
        self.addSubview(headerView)
        self.addSubview(infoStackView)
    }
    
    private func addViewConstraints () {
        timeLbl.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(43)
            make.leading.top.equalToSuperview().inset(16)
        }
        avatarImg.snp.makeConstraints { (make) in
            make.trailing.top.equalToSuperview().inset(16)
            make.width.height.equalTo(44)
        }
        headerView.snp.makeConstraints { (make) in
            make.top.equalTo(avatarImg.snp.bottom).offset(21)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(40)
        }
        infoStackView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(headerView.snp.bottom).offset(20)
            make.bottom.equalToSuperview().inset(16)
        }
    }
}
